package com.restaurant.application.util.wx;

import com.nebutown.commonunits.utils.JacksonUtils;
import com.nebutown.facilities.F;

import java.util.*;

public class WxFromIdUtil {

    private static final String APPLET_MEMBER_FORM_ID_KEY = "APPLET_MEMBER_FORM_ID_KEY_";

    //缓存默认七天
    private static final Integer APPLET_MEMBER_FORM_ID_TIME = 604800;


    public static Map<String, Long> saveOrUpdateFormId(String key, String formId) throws Exception {
        if (Objects.isNull(F.getICache().get(APPLET_MEMBER_FORM_ID_KEY + key))) {
            Map<String, Long> map = new LinkedHashMap<>();
            map.put(formId, System.currentTimeMillis() + APPLET_MEMBER_FORM_ID_TIME * 1000);
            F.getICache().put(APPLET_MEMBER_FORM_ID_KEY + "_${_request.memberId}", JacksonUtils.obj2json(map), APPLET_MEMBER_FORM_ID_TIME);
            return map;
        } else {
            Map<String, Long> fromIdMap = JacksonUtils.json2pojo(F.getICache().get(APPLET_MEMBER_FORM_ID_KEY + key), Map.class);
            if (fromIdMap != null) {
                if (fromIdMap.get(formId) == null) {
                    fromIdMap.put(formId, System.currentTimeMillis() + APPLET_MEMBER_FORM_ID_TIME * 1000);
                    F.getICache().put(APPLET_MEMBER_FORM_ID_KEY + key, JacksonUtils.obj2json(fromIdMap), APPLET_MEMBER_FORM_ID_TIME);
                }
                return fromIdMap;
            } else {
                Map<String, Long> map = new LinkedHashMap<>();
                map.put(formId, System.currentTimeMillis() + APPLET_MEMBER_FORM_ID_TIME * 1000);
                F.getICache().put(APPLET_MEMBER_FORM_ID_KEY + key, JacksonUtils.obj2json(map), APPLET_MEMBER_FORM_ID_TIME);
                return map;
            }
        }

    }

    public static String getMemberFormId(String key) throws Exception {
        Map<String, Long> fromIdMap = JacksonUtils.json2pojo(F.getICache().get(APPLET_MEMBER_FORM_ID_KEY + key), Map.class);
        if (fromIdMap != null) {
            Iterator<Map.Entry<String, Long>> iterator = fromIdMap.entrySet().iterator();
            long nowTime = System.currentTimeMillis();
            Map.Entry<String, Long> tailFirst = null;

            List<String> removerKey = new ArrayList<>();
            for (Map.Entry<String, Long> entry : fromIdMap.entrySet()) {
                if (entry.getValue() > nowTime) {
                    tailFirst = entry;
                    break;
                } else {
                    removerKey.add(entry.getKey());
                }
            }

            /*
             * 清除过期key
             */
            for (String k : removerKey) {
                fromIdMap.remove(k);
            }

            Map.Entry<String, Long> tailLast = null;
            while (iterator.hasNext()) {
                tailLast = iterator.next();
            }
            if (tailLast == null || (tailFirst != null && tailLast != null && tailFirst.getKey() == tailLast.getKey())) {
                F.getICache().delete(APPLET_MEMBER_FORM_ID_KEY + key);
            } else {
                fromIdMap.remove(tailFirst.getKey());
                long time = tailLast.getValue();
                if (nowTime > time) {
                    F.getICache().delete(APPLET_MEMBER_FORM_ID_KEY + key);
                } else {
                    long cacheTime = time - nowTime;
                    F.getICache().put(APPLET_MEMBER_FORM_ID_KEY + key, JacksonUtils.obj2json(fromIdMap), (int) cacheTime);
                }
            }
            if (tailFirst != null && tailFirst.getValue() > nowTime) {
                return tailFirst.getKey();
            }
            return null;
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        try {
//            saveOrUpdateFormId("agoeil", "oleisvks");
            getMemberFormId("agoeil");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
